package controllers;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import models.Unit;
import models.User;
import play.data.validation.Email;
import play.data.validation.Required;
import play.mvc.Controller;
import play.mvc.With;
import play.db.jpa.Blob;

@With(Secure.class)
public class Users extends Controller {
	public static void index() {
		render();
	}

	public static void Register(String FirstName, String LastName,
			String UserName, String Password, int ssn, String Address,
			@Email String Email, int HomeNumber, int MobileNumber,
			String Country, Blob photo) {

		if (validation.hasErrors()) {
			ArrayList<User> listuser = (ArrayList) User.findAll();
			for (int i = 0; i < listuser.size(); i++) {
				if (listuser.get(i).Email.equalsIgnoreCase(Email)) {
					flash.error("Oops! It appears that the Email has been already used before to create an account . Please choose another one.");
					
					render();

				}
				if (listuser.get(i).UserName.equalsIgnoreCase(UserName)) {
					flash.error("Oops! It appears that this username is already in use . Please choose another one.");

					render();
				}
				
			}
			
		
			LinkedList<Object> attributes = new LinkedList();
			attributes.addLast(FirstName);
			attributes.addLast(LastName);
			attributes.addLast(UserName);
			attributes.addLast(Password);
			attributes.addLast(ssn);
			attributes.addLast(Address);
			attributes.addLast(Email);
			attributes.addLast(HomeNumber);
			attributes.addLast(MobileNumber);
			attributes.addLast(Country);
			User newUser = new User(attributes);
			newUser.save();
			System.out.println(photo);
			newUser.photo = photo;
			newUser.save();

			createdUser(newUser);
		} else {
			render();
		}

	}

	public static void userPhoto(long id) {
		final User user = User.findById(id);
		notFoundIfNull(user);
		response.setContentTypeIfNotSet(user.photo.type());
		renderBinary(user.photo.get());
	}

	public static void EditUser(long id) {

		User x = User.findById(id);
		render(x, id);

	}

	public static void Edit(Long id, String FirstName, String LastName,
			String UserName, String Password, int ssn, String Address,
			String Email, int HomeNumber, int MobileNumber, String Country,
			Blob photo) {

		if (FirstName != null) {
			ArrayList<User> listuser = (ArrayList) User.findAll();
			for (int i = 0; i < listuser.size(); i++) {
				if (listuser.get(i).UserName.equalsIgnoreCase(UserName))
					flash.error("oops the username you choose is already used. please change it");
				if (listuser.get(i).Email.equalsIgnoreCase(Email))
					flash.error("oops the mail you choose is already used. please change it");

			//render();
			}
			User u = User.findById(id);
			u.FirstName = FirstName;
			u.LastName = LastName;
			u.UserName = UserName;
			u.Password = Password;
			u.ssn = ssn;
			u.Address = Address;
			u.Email = Email;
			u.HomeNumber = HomeNumber;
			u.MobileNumber = MobileNumber;
			u.Country = Country;
			u.photo = photo;
			u.save();
			ViewUser(id);
		} else {
			ViewUser(id);
		}
	}

	public static void createdUser(User newUser) {

		render(newUser);

	}

	public static void ViewAllUsers() {

		ArrayList<User> allusers = (ArrayList) User.findAll();
		for (int i = 0; i < allusers.size(); i++) {
			if (allusers.get(i).UserName.equalsIgnoreCase("admin")) {
				allusers.remove(i);
			}
		}
		render(allusers);

	}

	public static void ViewUser(long id) {

		User x = User.findById(id);
		
		List <Unit> unitlist =  x.units;
		render(x,unitlist);
	}

	public static void DeleteUser(long id) {
		User user = User.findById(id);
		user.delete();
		ViewAllUsers();
	}

}
